package dse;

import java.util.Iterator;

public class Pair<A,B> {
	public A first;
	public B second;
	
	public Pair (A first, B second) {
		this.first = first;
		this.second = second;
	}
	
	
	
	@Override
	@SuppressWarnings("unchecked")
	public boolean equals (Object obj) {
		if (obj instanceof Pair) {
			Pair<A,B> other = (Pair<A,B>)obj;
			return (this.first == other.first) && (this.second == other.second);
		}
		return false;
	}

	@Override
	public int hashCode () {
		return first.hashCode() ^ second.hashCode();
	}

	@Override
	public String toString () {
		return "(" + first + ", " + second + ")";
	}



	public static <A,B> Iterable<A> selectFirst (Iterable<Pair<A,B>> iterable) {
		final Iterator<Pair<A,B>> orig = iterable.iterator();
		return new Iterable<A> () {
			public Iterator<A> iterator () {
				return new Iterator<A> () {
					public A next () {
						Pair<A,B> v = orig.next();
						return v.first;
					}
					public boolean hasNext () {
						return orig.hasNext();
					}
					public void remove () {
						orig.remove();
					}
				};
			}
		};
	}
	public static <A,B> Iterable<B> selectSecond (Iterable<Pair<A,B>> iterable)
	{
		final Iterator<Pair<A,B>> orig = iterable.iterator();
		return new Iterable<B> () {
			public Iterator<B> iterator () {
				return new Iterator<B> () {
					public B next () {
						Pair<A,B> v = orig.next();
						return v.second;
					}
					public boolean hasNext () {
						return orig.hasNext();
					}
					public void remove () {
						orig.remove();
					}
				};
			}
		};
	}
}
